package com.goldgov.basedata.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.basedata.dao.BaseDataDao;
import com.goldgov.basedata.service.BaseData;
import com.goldgov.basedata.service.BaseDataOrg;
import com.goldgov.basedata.service.BaseDataQuery;
import com.goldgov.basedata.service.BaseDataService;
import com.goldgov.basedata.service.IBaseDataOrgService;
@Service
public class BaseDataServiceImpl implements BaseDataService{

	@Autowired
	private BaseDataDao baseDataDao;
	
	@Override
	public List<BaseData> getBaseDataList(BaseDataQuery query) {
		return baseDataDao.getBaseDataListPageSize(query);
	}

	@Override
	public BaseData getBaseData(String dataID) {
		return baseDataDao.getBaseData(dataID);
	}

	@Autowired
	private IBaseDataOrgService baseDataOrgService;
	@Override
	public void addBaseData(BaseData baseData) {
		//根据typeCode,datakey,datavalue查询是否已存在
		BaseDataQuery query=new BaseDataQuery();
		query.setSearchDataKey(baseData.getDataKey());
		query.setSearchDataValue(baseData.getDataValue());
		query.setSearchTypeCode(baseData.getTypeCode());
		List<BaseData> list=baseDataDao.findBaseDataList(query);
		BaseDataOrg baseDataOrg=new BaseDataOrg();
		if(list==null||list.size()==0) {
			baseDataDao.addBaseData(baseData);
			baseDataOrg.setDataIDs(new String[] {baseData.getDataID()});
		}else {
			baseDataOrg.setDataIDs(new String[] {list.get(0).getDataID()});
		}
		//加入关联表
		baseDataOrg.setOperationUserId(baseData.getUserID());
		baseDataOrg.setOrganizationIds(new String[] {baseData.getOrgID()});
		baseDataOrgService.addBaseDataOrg(baseDataOrg);
	}

	@Override
	public void deleteBaseData(String dataID) {
		baseDataDao.deleteBaseData(dataID);
		
	}

	@Override
	public void updateBaseData(BaseData baseData) {
	   baseDataDao.updateBaseData(baseData);
	}

	@Override
	public void deleteBaseDataByTypeCode(String typeCode) {
		baseDataDao.deleteBaseDataByTypeCode(typeCode);
	}

	@Override
	public List<BaseData> findBaseDataList(BaseDataQuery query) {
		return baseDataDao.findBaseDataList(query);
	}

	@Override
	public String findOrgIdByScopeCode(String scopeCode) {
		return baseDataDao.findOrgIdByScopeCode(scopeCode);
	}

}
